var roleType, unitId;
// 今日的年月，默认查询
var date = new Date;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" + month : month);
var defaultDate = (year.toString() + '-' + month.toString());

var elDate = document.getElementById('date');
elDate.innerText = defaultDate;
//分页初始化
var pageNum = 1;
var pageSize = 10;
var total;
mui.init({
	pullRefresh: {
		container: '#pullrefresh',
		/*down: { //下拉刷新
			callback: pulldownRefresh,
			style: mui.os.android ? "circle" : "default"
		},*/
		up: { //上拉加载
			contentinit: '',
			contentrefresh: '正在加载...',
			contentnomore: '没有更多了',
			callback: pullupRefresh,
		}
	}
})

mui.plusReady(function() {
	roleType = localStorage.getItem("roleType");
	unitId = localStorage.getItem("unitId");
	loadData(elDate.innerText);

	//点击进入单位设备列表
	/*mui("#list_wrap").on('tap', 'li', function() {
		alert("ee");
		var id = this.getAttribute("data-id");
		openNew("equipList.html", {
			unitId: id
		});
	})*/

	//点击进入详情
	mui("#list_warp").on('tap', 'li', function() {
		var id = this.getAttribute("data-id");
		openNew("equipList.html", {
			unitId: id,
		});
	})

	//点击tab重新加载数据
	/*	mui(".mui-segmented-control").on('tap', 'a', function() {
			var type = this.getAttribute("data-type");
			loadData();
		})*/

	//点击获取数据
	document.getElementById('getData').addEventListener('tap', function() {
		loadData(elDate.innerText);
	})

	//选择月份
	document.getElementById('selectMonth').addEventListener('tap', function(e) {
		var _self = this;
		_self.picker = new mui.DtPicker({
			"type": "month"
		});
		_self.picker.show(function(re) {
			var date = document.getElementById('date');
			date.innerHTML = re.text;
		});
	})

});

//下拉刷新具体业务实现
function pulldownRefresh() {
	//重置页码
	pageNum = 1;
	loadData(elDate.innerText);
}
// 上拉加载具体业务实现
function pullupRefresh() {
	loadData(elDate.innerText, true);
}

function endPullRefresh(stopup, id) { //上拉加载、下拉刷新动画结束
	//stopup 上拉加载
	//stopdown 下拉刷新
	//是否重置插件
	//id 容器id
	if (id == undefined) //容器id
		id = "pullrefresh";

	setTimeout(function() {
		//停止下拉刷新
		//mui('#' + id).pullRefresh().endPulldownToRefresh(true);
		//重置
		mui('#' + id).pullRefresh().refresh(true);
		if (stopup != undefined) { //不为空表示，页面包含 上拉加载事件
			//停止上拉加载
			mui('#' + id).pullRefresh().endPullupToRefresh(stopup);
		}

	}, 1000);
}
// 统计评估-单位列表
function loadData(time,isnextpage, isreload) {
	if (isnextpage&&(pageNum * pageSize < total)) { //加载下一页
		pageNum++;
	} else if (isreload) { //重新加载当前页
		pageNum = curr_pageno;
	} else if (pageNum == 0) {
		pageNum = 1; //未加载过
	} else {
		pageNum = 1; //默认加载第一页
	}
	var showload = pageNum == 1;
	var isappend = pageNum > 1 ? true : false;
	log(pageNum + "," + total + "," + showload + "," + isappend)
	if (pageNum * pageSize >= total) {
		endPullRefresh(true);
	}
	request("/app/alarm/statistics/unit/list", "GET", {
		time: time,
		roleType: roleType,
		manageId: unitId,
		pageNum: pageNum,
		pageSize: pageSize
	}, function(res) {
		pageNum = res.pager.pageNum;
		pageSize = res.pager.pageSize;
		total = res.pager.total;

		var nomore = true;
		if (res.code == 0) {
			render("#list_warp", "unitList", res, isappend);
			nomore = pageNum * pageSize >= total;
		} else {
			appUI.showTopTip(json.msg);
		}
		
		endPullRefresh(nomore);
	}, true, function() {
		endPullRefresh();
	});
}

/*
// 统计评估-设备列表
function getDetail(){
	request("/app/alarm/statistics/equipment/list", "GET",{
		unitId: '1'
	}, function(res) {
		
	}, false, function() {
		
	});
}*/
